import Indicator from "./Indicator";
import FSM from "./lib/FSM";
import FSMConditionTrigger from "./lib/FSMConditionTrigger";
import MonsterBlackboard from "./MonsterBlackboard";
import MonsterChaseState from "./MonsterChaseState";
import MonsterPatrolState from "./MonsterPatrolState";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FSMMonster extends cc.Component {
    public goldRange: number = 300;
    public monsterRange: number = 100;

    public player: cc.Node = null;
    public gold: cc.Node = null;
    public indicator: Indicator = null;
    private _fsm: FSM;

    protected onLoad(): void {
        this.indicator = this.node.getChildByName("indicator").getComponent(Indicator);
        this.gold = cc.find("Canvas/gold", cc.director.getScene());
        this.player = cc.find("Canvas/player", cc.director.getScene());
        //初始化状态
        this._fsm = new FSM();
        this._fsm.node = this.node;
        this._fsm.init(new MonsterBlackboard());

        let patrol = this._fsm.addState(new MonsterPatrolState());
        let chase = this._fsm.addState(new MonsterChaseState());

        let transition = patrol.addTransition(chase.id);
        transition.addCondition(new FSMConditionTrigger("isHeroInRange"));

        transition = chase.addTransition(patrol.id);
        transition.addCondition(new FSMConditionTrigger("isHeroOutGoldRange"));
    }

    protected update(dt: number): void {
        this._fsm.update(dt);
    }
}